/**
 * Copyright 2010 Jake Jackson
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jake.nb.ext.ant.hints.impl;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import javax.swing.text.Document;
import org.jake.nb.ext.ant.hints.AntHint;
import org.jake.nb.ext.ant.hints.Configuration;
import org.jake.nb.ext.ant.hints.Paths;
import org.jake.nb.ext.ant.hints.SpanContainer;
import org.jake.nb.ext.ant.parser.DocumentUtilities;
import org.jake.nb.ext.ant.parser.Span;
import org.jake.nb.ext.ant.parser.TargetSpan;
import org.netbeans.modules.editor.NbEditorUtilities;
import org.netbeans.spi.editor.hints.ChangeInfo;
import org.netbeans.spi.editor.hints.ErrorDescription;
import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
import org.netbeans.spi.editor.hints.Fix;
import org.netbeans.spi.editor.hints.Severity;
import org.openide.util.NbBundle;
import org.openide.util.lookup.ServiceProvider;

/**
 * An ant hinte checking there's no whitespace in the target's name.
 * @version 0.
 * @author JakeJ
 * @since
 */
@ServiceProvider(service=AntHint.class, path="org-jake-nb-ext-ant-hints")
public class WhitespaceInTargetName implements AntHint {

  private Configuration configuration;

  public WhitespaceInTargetName() {
    configuration = new Configuration(WhitespaceInTargetName.class.getCanonicalName(), "Whitespace in target name",
            "Indicates a target's name attribute contains a whitespace. This isn't invalid at all, but the target will"
            + " not be found by a calling task, like depends or antcall.",
            true, Severity.WARNING, Paths.TARGET);
  }

  @Override
  public Collection<ErrorDescription> getErrors(final SpanContainer spanContainer) {
    final Collection<ErrorDescription> errors = new HashSet<ErrorDescription>();
    for (final Span span : spanContainer.getTargetSpans().getSpans()) {
      final TargetSpan targetSpan = (TargetSpan)span;
      final String name = targetSpan.getName();
      final String trimmedName = name.trim();
      // if the name is empty no need to check, instead another hint will indicate this
      if(trimmedName.length() > 0 && name.length() > trimmedName.length()) {
        errors.add(createError(spanContainer.getDocument(), targetSpan));
      }
    }
    return errors;
  }

  @Override
  public Configuration getConfiguration() {
    return configuration;
  }

  private ErrorDescription createError(final Document document, final TargetSpan span)  {

    return ErrorDescriptionFactory.createErrorDescription(configuration.getSeverity(), NbBundle.getMessage(this.getClass(), "Desc_Err_WhitespaceInTargetName"),
                Collections.<Fix>singletonList(new RemoveWhitespaceFix(document, span)),
                document, NbEditorUtilities.getLine(document, span.getStart(), true).getLineNumber() + 1);
  }

  private static class RemoveWhitespaceFix implements Fix {
    private final TargetSpan span;
    private final Document document;

    RemoveWhitespaceFix(final Document document, final TargetSpan targetSpan) {
      this.document = document;
      this.span = targetSpan;
    }

    @Override
    public String getText() {
      return NbBundle.getMessage(this.getClass(), "Fix_WhitespaceInTargetName"); // NOI18N
    }

    @Override
    public ChangeInfo implement() throws Exception {
      final ChangeInfo info = new ChangeInfo();
      final Span nameSpan = span.getNameSpan();
      final String nameText = document.getText(nameSpan.getStart(), nameSpan.getLength());
      final int startQuote = nameText.indexOf('"');
      final String newValue = nameText.substring(0, startQuote + 1) + span.getName().trim() + '"'; // NOI18N

      document.remove(nameSpan.getStart(), nameSpan.getLength());
      document.insertString(nameSpan.getStart(), newValue, null);
      DocumentUtilities.save(document);
      return info;
    }

  }

}
